package com.example.concurrent.chapter05;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.IntStream;

import static java.lang.Thread.currentThread;
import static java.util.concurrent.ThreadLocalRandom.current;

/**
 * @author Liuqz
 * @date 2018/11/21 23:34
 */
public class BooleanLockTest {

    private final Lock lock = new BooleanLock();

    public void syncMethod() {

        //加锁
        try {
            lock.lock();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        int randomInt = current().nextInt(10);
        System.out.println(currentThread() + " get the lock.");
        try {
            TimeUnit.SECONDS.sleep(randomInt);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void syncMethodTimeoutable() {
        try {
            lock.lock(1000);
            System.out.println(currentThread() + " get the lock");
            int randomInt = current().nextInt(10);
            TimeUnit.SECONDS.sleep(randomInt);
        } catch (InterruptedException | TimeoutException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        BooleanLockTest blt = new BooleanLockTest();

//        IntStream.range(0, 10).mapToObj(i -> new Thread(blt::syncMethod)).forEach(Thread::start);

        new Thread(blt::syncMethod,"T1").start();
        TimeUnit.SECONDS.sleep(2);
        Thread t2 = new Thread(blt::syncMethodTimeoutable,"T2");
        t2.start();
        TimeUnit.SECONDS.sleep(10);
        t2.interrupt();
    }
}
